﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBO.Data;
using PBO.Network;
using PBO.Game;
using PBO.Network.Enumerations;

namespace PBO.Game.BattleClass
{
    public partial class Battle
    {
        private void CalculateMoveEffect(Pokemon user, Pokemon target, Move selMove, bool moveCall)
        {
            var selMoveType = GetAttackMoveType(selMove, user);
            if (target.HealthPower == 0)
            {
                return;
            }
            int addEffOdds = selMove.Data.AddEffectOdds;
            if (user.GetSelTrait() == Trait.SereneGrace)
                addEffOdds *= 2;

            //if (0 < addEffOdds && addEffOdds < 100 && !this.RandomHappen(addEffOdds)) return;

            if (!(target.GetSelTrait() == Trait.ShieldDust && user.GetSelTrait() != Trait.MoldBreaker))
            {
                switch (selMove.Data.AddEffect1)
                {
                    case MoveAdditionalEffect.一定几率烧伤:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Burn, user);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率麻痹:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Paralysis, user);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率冻结:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Freeze, user);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率中毒:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Poison, user);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率剧毒:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Toxin, user);
                        }
                        break;
                    //case MoveAdditionalEffect.一定几率睡眠:
                    //    if (_random.NextDouble() < moveAdditionalEffectOdds)
                    //    {
                    //        target.SetState(PokemonState.Sleep, user);
                    //    }
                    //    break;
                    case MoveAdditionalEffect.一定几率烧伤或害怕:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Burn, user);
                        }
                        if (this.RandomHappen(addEffOdds))
                            target.terrify(user);
                        break;
                    case MoveAdditionalEffect.一定几率麻痹或害怕:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Paralysis, user);
                        }
                        if (this.RandomHappen(addEffOdds))
                            target.terrify(user);

                        break;
                    case MoveAdditionalEffect.一定几率冻结或害怕:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Freeze, user);
                        }
                        if (this.RandomHappen(addEffOdds))
                            target.terrify(user);

                        break;
                    case MoveAdditionalEffect.三角攻击:
                        if (this.RandomHappen(addEffOdds))
                        {
                            PokemonState[] addStates = { PokemonState.Burn, PokemonState.Paralysis, PokemonState.Freeze };
                            target.SetState(addStates[this.GetRandomInt(0, 2)], user);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率混乱:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.confuse(moveCall, user);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率害怕:
                        if (this.RandomHappen(addEffOdds))
                            target.terrify(user);
                        break;
                    case MoveAdditionalEffect.一定几率速度减一:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.AbilityLvDownChanged(StatType.Speed, user, 1, true);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率特防减一:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.AbilityLvDownChanged(StatType.SpDef, user, 1, true);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率特防减二:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.AbilityLvDownChanged(StatType.SpDef, user, 2, true);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率物攻减一:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.AbilityLvDownChanged(StatType.Atk, user, 1, true);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率物防减一:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.AbilityLvDownChanged(StatType.Def, user, 1, true);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率命中减一:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.AbilityLvDownChanged(StatType.Accuracy, user, 1, false);
                        }
                        break;
                    case MoveAdditionalEffect.一定几率特攻减一:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.AbilityLvDownChanged(StatType.SpAtk, user, 1, false);
                        }
                        break;
                    case MoveAdditionalEffect.秘密之力:
                        if (this.RandomHappen(addEffOdds))
                        {
                            switch (Ground.Terrain)
                            {
                                case BattleTerrain.Stadium:
                                    target.SetState(PokemonState.Paralysis, user);
                                    break;
                                case BattleTerrain.Grass:
                                    target.SetState(PokemonState.Sleep, user);
                                    break;
                                case BattleTerrain.Flat:
                                case BattleTerrain.Sand:
                                    target.AbilityLvDownChanged(StatType.Accuracy, user, 1, false);
                                    break;
                                case BattleTerrain.Mountain:
                                case BattleTerrain.Cave:
                                    target.terrify(user);
                                    break;
                                case BattleTerrain.Water:
                                    target.AbilityLvDownChanged(StatType.Atk, user, 1, false);
                                    break;
                                case BattleTerrain.SnowField:
                                    target.SetState(PokemonState.Freeze, user);
                                    break;
                            }
                        }
                        break;
                }
                switch (selMove.Data.AddEffect2)
                {
                    case MoveAdditionalEffect.雷电:
                        if (this.RandomHappen(30))
                        {
                            target.SetState(PokemonState.Paralysis, user);
                        }
                        break;
                    case MoveAdditionalEffect.跳起后攻击:
                        if (this.RandomHappen(addEffOdds))
                        {
                            target.SetState(PokemonState.Paralysis, user);
                        }
                        break;
                    case MoveAdditionalEffect.暴风雪:
                        if (this.OneNth(10))
                        {
                            target.SetState(PokemonState.Freeze, user);
                        }
                        break;
                    case MoveAdditionalEffect.下马威:
                        target.terrify(user);
                        break;
                }
                //恶臭
                if (selMove.Contact && user.GetSelTrait() == Trait.Stench && target.GetSelTrait() != Trait.MoldBreaker)
                {
                    if (this.OneNth(10))
                        target.terrify(user);
                }
            }

            if (target.State == PokemonState.Freeze && selMoveType == BattleType.Fire)
            {
                target.State = PokemonState.No;
                log.AppendMsg("DeFRZ", target.CustomName);
                //log.AddText(target.CustomName + "解冻了！");
            }

            switch (selMove.Data.AddEffect1)
            {
                case MoveAdditionalEffect.夺取果实:
                    if (target.GetItem(true).IsBerry() && (target.GetSelTrait() != Trait.StickyHold || user.GetSelTrait() == Trait.MoldBreaker))
                    {
                        log.AppendMsg("EatDefenderBerry", user.CustomName, target.GetItem(true));
                        //log.AddText(user.CustomName + "夺取了" + target.CustomName + "的果实！");
                        user.SetItem(target.GetItem(true));
                        user.RaiseItem();
                        target.SetItem(Item.None);
                    }
                    break;
                //case MoveAdditionalEffect.燃尽果实:
                //    if (Item.果实1 <= target.GetItem(true) && target.GetItem(true) <= Item.果实64)
                //    {
                //        target.SetItem(Item.无);
                //        log.AddText(string.Format("{0}的果实被烧掉了", target.CustomName));
                //    }

                //    break;
                case MoveAdditionalEffect.夺取道具:
                    if (user.GetItem(true) == Item.None && target.GetItem(true) != Item.None && (target.GetSelTrait() != Trait.StickyHold || user.GetSelTrait() == Trait.MoldBreaker))
                    {
                        log.AppendMsg("Thief", user.CustomName, target.CustomName, user.GetItem(true));
                        //log.AddText(user.CustomName + "夺取了" + target.CustomName + "的道具！");
                        user.SetItem(target.GetItem(true));
                        target.SetItem(Item.None);
                    }
                    break;
                case MoveAdditionalEffect.打落:
                    if (target.GetItem(true) != Item.None && (target.GetSelTrait() != Trait.StickyHold || user.GetSelTrait() == Trait.MoldBreaker))
                    {
                        log.AppendMsg("KnockOff", user.CustomName, target.CustomName, target.GetItem(true));
                        //log.AddText(target.CustomName + "的" + target.GetItem(true).ToString() + "被打落了！");
                        target.SetItem(Item.None);
                    }
                    break;
                case MoveAdditionalEffect.暂时束缚:
                    if (!target.BattleState.constraint)
                    {
                        target.BattleState.constraint = true;
                        if (user.GetItem() == Item.GripClaw)
                        {
                            target.BattleState.constraintTurn = 5;
                        }
                        else
                        {
                            target.BattleState.constraintTurn = _random.Next(2, 6);
                        }
                        target.BattleState.constraintCounter = 0;
                        target.BattleState.constraintBy = GetPokemonIndex(user);
                        log.AppendMsg("EnCantSelectWithdraw", target.CustomName);
                        //log.AppendText(target.CustomName + "不能逃走了！");
                    }
                    break;
            }
            switch (selMove.Data.AddEffect2)
            {
                case MoveAdditionalEffect.对象睡眠伤害加倍:
                    if (target.State == PokemonState.Sleep)
                    {
                        target.State = PokemonState.No;
                        log.AppendMsg("DeSLP", target.CustomName);
                        //log.AddText(target.CustomName + "醒过来了！");
                    }
                    break;
                case MoveAdditionalEffect.对象麻痹伤害加倍:
                    if (target.State == PokemonState.Paralysis)
                    {
                        target.State = PokemonState.No;
                        log.AppendMsg("DePAR", target.CustomName);
                        //log.AddText(target.CustomName + "解除了麻痹！");
                    }
                    break;
            }
        }

    }
}
